import {
  queryTotal,
  queryStatisticalIndex,
  queryDevices,
  modifyDevice,
  createCylinder,
  modifyCylinder,
  queryCylinder,
  createProtected,
  modifyProtected,
  queryProtected
} from './service';
import { routerRedux } from 'dva/router';
import { DICT } from './enum';
import { queryDictList } from '@/services/dict';
import { message } from 'antd';

export default {
  namespace: 'archives_devices',

  state: {
    total: {},
    cylinderCond: {},
    protectedCond: {},
    dict: {},
    outdoors: {},
    indoors: {},
    spray: {},
    gas: {},
    foam: {},
    fireReport: {},
    emitSmoke: {},
    preventSmoke: {},
    otherSys: {},
    cylinder: {},
    protect: {},
  },

  effects: {
    *queryTotal(_, { call, put }) {
      const response = yield call(queryTotal);
      yield put({ type: 'setTotal', payload: response });
    },
    *queryStatisticalIndex(_, { call, put }) {
      const response = yield call(queryStatisticalIndex);
      yield put({ type: 'setStatisticalIndex', payload: response });
    },
    *queryDict({ payload }, { call, put }) {
      const dict = yield call(queryDictList, payload);
      yield put({ type: 'setDict', payload: dict })
    },
    *queryDevices(_, { call, put }) {
      const devices = yield call(queryDevices);
      yield put({ type: 'setDevices', payload: devices });
    },
    *modifyDevice({ payload }, { call, put }) {
      const response = yield call(modifyDevice, payload);
      response ? message.success('保存成功', 1.5) : message.error('保存失败', 1.5);
      yield put({ type: 'queryDevices' });
    },
    *initCylinderCond(_, { put }) {
      yield put({ type: 'setCylinderCond', payload: { pageSize: 10 } });
      yield put({ type: 'queryCylinder' })
    },
    *updateCylinderCond({ payload }, { put, select }) {
      const cond = yield select(state => state.archives_devices.cylinderCond);
      const params = { ...cond, ...payload };
      yield put({ type: 'setCylinderCond', payload: params });
      yield put({ type: 'queryCylinder' })
    },
    *createCylinder({ payload }, { call, put }) {
      yield call(createCylinder, payload);
      // yield put({ type: 'queryCylinder' });
    },
    *modifyCylinder({ payload }, { call, put }) {
      yield call(modifyCylinder, payload);
      // yield put({ type: 'queryCylinder' });
    },
    *queryCylinder({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.archives_devices.cylinderCond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryCylinder, params);
      yield put({ type: 'setCylinder', payload: { list, total } })
    },
    *initProtectedCond(_, { put }) {
      yield put({ type: 'setProtectedCond', payload: { pageSize: 10 } });
      yield put({ type: 'queryProtected' })
    },
    *updateProtectedCond({ payload }, { put, select }) {
      const cond = yield select(state => state.archives_devices.protectedCond);
      const params = { ...cond, ...payload };
      yield put({ type: 'setProtectedCond', payload: params });
      yield put({ type: 'queryProtected' })
    },
    *createProtected({ payload }, { call, put }) {
      yield call(createProtected, payload);
      // yield put({ type: 'queryProtected' });
    },
    *modifyProtected({ payload }, { call, put }) {
      yield call(modifyProtected, payload);
      // yield put({ type: 'queryProtected' });
    },
    *queryProtected({ payload = { pageNum: 1 } }, { call, put, select }) {
      const cond = yield select(state => state.archives_devices.protectedCond);
      const params = { ...payload, ...cond };
      if (!params.pageSize) params.pageSize = 10;
      const { list = [], total } = yield call(queryProtected, params);
      yield put({ type: 'setProtected', payload: { list, total } });
    }
  },

  reducers: {
    setCylinderCond(state, action) {
      return {
        ...state,
        cylinderCond: action.payload
      }
    },
    setProtectedCond(state, action) {

      return {
        ...state,
        protectedCond: action.payload
      }
    },
    setTotal(state, action) {
      return {
        ...state,
        total: action.payload
      }
    },
    setStatisticalIndex(state, action) {
      return {
        ...state,
        statisticalIndex: action.payload
      }
    },
    setDict(state, action) {
      return {
        ...state,
        dict: action.payload
      }
    },
    setDevices(state, action) {
      const {
        outdoorItem, indoorItem, autoOutfireItem, gasOutfireItem, foamSystemItem,
        autoFireAlarmSystemItem, smokeExhaustSystemItem, smokeControlSystemItem, otherSystemItem
      } = action.payload;
      return {
        ...state,
        outdoors: Array.isArray(outdoorItem) && outdoorItem[0],
        indoors: Array.isArray(indoorItem) && indoorItem[0],
        spray: Array.isArray(autoOutfireItem) && autoOutfireItem[0],
        gas: Array.isArray(gasOutfireItem) && gasOutfireItem[0],
        foam: Array.isArray(foamSystemItem) && foamSystemItem[0],
        fireReport: Array.isArray(autoFireAlarmSystemItem) && autoFireAlarmSystemItem[0],
        emitSmoke: Array.isArray(smokeExhaustSystemItem) && smokeExhaustSystemItem[0],
        preventSmoke: Array.isArray(smokeControlSystemItem) && smokeControlSystemItem[0],
        otherSys: Array.isArray(otherSystemItem) && otherSystemItem[0],
      }
    },
    setCylinder(state, action) {
      return {
        ...state,
        cylinder: action.payload
      }
    },
    setProtected(state, action) {
      return {
        ...state,
        protect: action.payload
      }
    },
  }
}
